package com.lly.netcross.client;

import com.lly.netcross.thread.ClientThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;

public class TcpClient {
    private Scanner scanner = new Scanner(System.in);
    private static Logger logger = LoggerFactory.getLogger(TcpClient.class);
    private Socket socket;
    private BufferedWriter bw;
    private BufferedReader br;
//    private String serverIp = "107.174.70.22";
    private String serverIp = "127.0.0.1";
    private int serverPort = 8080;
    private String localIp = "127.0.0.1";
    private int localPort ;
    private boolean isWaitInput = true;
    public TcpClient() {
    }

    public TcpClient(int localPort) {
        this.localPort = localPort;
    }

    public void start(){
        socket = new Socket();
        try {
            socket.setReuseAddress(true);
            socket.connect(new InetSocketAddress(serverIp,serverPort));
            localPort = socket.getLocalPort();
            localIp = socket.getLocalAddress().getHostAddress();
            if (logger.isInfoEnabled()){
                logger.info("localPort:" + localPort);
            }
            logger.info("scanner notWait ! waiting cross or scanner conn crossing!");
            bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            try {
                while (true){
                    if (process()){
                        break;
                    }
                }
            }finally {
                if (bw != null){
                    bw.close();
                }
                if (br != null){
                    br.close();
                }
                if (socket != null){
                    socket.close();
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private boolean process() throws IOException {
        String in = null;
        if (isWaitInput){
            //客户端输入
            in=scanner.next();
            bw.write(in+"\n");
            bw.flush();
            if ("notWait".equals(in)){
                isWaitInput = false;
            }
        }
        String info = br.readLine();
        System.out.println(info);
        if(null != info){
            logger.info("server ask:"+info);
        }
        processLocalCommand(in);
        processRemoteCommand(info);

        return "exit".equals(in);

    }

    /**
     * 执行远程命令
     * @param info
     */
    private void processRemoteCommand(String info){
        if (null != info && info.startsWith("autoConn")){
            logger.info("server callback command ! starting connect other clint!");
        }
        String[] command = info.split("_");
        doPenetration(command[1],Integer.parseInt(command[2]));
    }

    /**
     * 执行本地命令
     * @param in
     * @throws IOException
     */
    private void processLocalCommand(String in) throws IOException {
        if("connTo".equals(in)){
            logger.info("input remote ip");
            String ip=scanner.next();
            logger.info("input remote port");
            int port = scanner.nextInt();
            bw.write("connectClient_" + ip + "_" + port+"\n");
            bw.flush();
            doPenetration(ip,port);
            isWaitInput = false;
        }


    }

    private void doPenetration(String ip,int port){
        ClientThread thread = new ClientThread(localIp, localPort, ip, port);
        thread.start();
        isWaitInput = false;
    }






}
